package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyComponent;
import com.yzy.fly.entity.role.FlyInterface;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface FlyInterfaceMapper extends FlyBasicMapper<FlyInterface> {

    @Select("select * from fly_interface a where 1=1")
    List<FlyInterface> selectAll();

    @Select("select * from fly_interface a where 1=1 and a.componentId = #{componentId} order by seq asc")
    List<FlyInterface> selectAllByComponentId(@Param("componentId") int componentId);


    @Select("select * from fly_interface a where 1=1 and a.systemId = #{systemId}")
    List<FlyInterface> selectAllBySystemId(@Param("systemId") int systemId);

    @Select("select * from fly_interface a where 1=1 and a.parentId = #{controllerId}")
    List<FlyInterface> selectAllByControllerId(@Param("controllerId") int controllerId);

    @Select("select * from fly_interface a where 1=1 and a.name = #{name}")
    FlyInterface selectOneByName(@Param("name") String name);

    @Select("select * from fly_interface a where systemId = #{systemId} order by a.packageName asc")
    List<FlyInterface> selectAllPackageNameAsc(@Param("systemId") Integer systemId);

    @Update("update fly_interface a set a.processId = #{processId} where a.id = #{businessId}")
    int updateProcessIdById(@Param("businessId")int businessId, @Param("processId")int processId);

    @Select({"",
        "select * from fly_component a where a.id in ",
        "(",
        "    select d.businessId from fly_process_node b,fly_process_line c,fly_process_node d where 1=1 ",
        "    and d.businessId is not null",
        "    and b.id =  c.`to`",
        "    and d.id =  c.`from`",
        "    and b.processId = c.processId",
        "    and b.processId = d.processId",
        "    and b.processId in (select e.id from fly_process e where e.id is not null and  e.businessId in (select f.id from fly_interface f where f.componentId = 20 ))",
        ")"
    })
    List<FlyComponent> selectAllFromBusinessIdByToBusinessId(@Param("processType") int processType,@Param("businessType") int type, @Param("businessId") int interfaceId);

}